package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbDictMapper;
import com.bwie.mapper.TbServerTypeMapper;
import com.bwie.pojo.TbDict;
import com.bwie.pojo.TbServerItem;
import com.bwie.pojo.TbServerType;
import com.bwie.service.TbServerItemService;
import com.bwie.mapper.TbServerItemMapper;
import com.bwie.utils.Result;
import com.bwie.vo.ItemPageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author 28242
* @description 针对表【tb_server_item(服务项表)】的数据库操作Service实现
* @createDate 2025-07-11 14:39:21
*/
@Service
public class TbServerItemServiceImpl extends ServiceImpl<TbServerItemMapper, TbServerItem>
    implements TbServerItemService{

    @Autowired
    private TbServerItemMapper tbServerItemMapper;

    @Autowired
    private TbServerTypeMapper tbServerTypeMapper;

    @Autowired
    private TbDictMapper tbDictMapper;

    @Override
    public Result queryItemList(ItemPageVo itemPageVo) {

        Page<TbServerItem> page = new Page<>(itemPageVo.getPageNum(),itemPageVo.getPageSize());

        QueryWrapper<TbServerItem> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(itemPageVo.getKey())){
            wrapper.lambda().like(TbServerItem::getItemName,itemPageVo.getKey());
        }
        if (itemPageVo.getType()!=null){
            wrapper.lambda().eq(TbServerItem::getServerTypeId,itemPageVo.getType());
        }


        Page<TbServerItem> itemPage = tbServerItemMapper.selectPage(page, wrapper);


        return Result.ok().setData(itemPage);
    }

    @Override
    public Result queryItemTypeList() {

        List<TbServerType> tbServerTypes = tbServerTypeMapper.selectList(null);

        return Result.ok().setData(tbServerTypes);
    }

    @Override
    public Result queryDictListByTyeId(Integer dictTypeId) {

        List<TbDict> tbDicts = tbDictMapper.selectList(
                new QueryWrapper<TbDict>().lambda().eq(TbDict::getDictType, dictTypeId)
        );


        return Result.ok().setData(tbDicts);
    }

    @Override
    public Result SaveServerItem(TbServerItem tbServerItem) {

        tbServerItem.setCreateTime(new Date());
        tbServerItem.setItemStatus(0);
        tbServerItemMapper.insert(tbServerItem);

        return Result.ok();
    }

    @Override
    public BigDecimal queryItemPriceByItemId(Integer itemId) {

        TbServerItem tbServerItem = tbServerItemMapper.selectById(itemId);
        return tbServerItem.getPrice();
    }


}




